const Mock = require('mockjs')

const listData = Mock.mock({
  'list|20': [{
    'id': '@id',
    status: '@integer(0,1)',
    author: '@cname',
    word: '@cparagraph(1, 3)'
  }]
})

module.exports = [

  // 列表接口
  {
    url: '/mock/content/inerview',
    type: 'get',
    response: config => {
      if (config.query.id) {
        console.log(config.query.id)
        // 单个查询
        const res = listData.list.find(item => item.id * 1 === config.query.id * 1)
        if (res) {
          return { success: true, message: '', data: res }
        } else {
          return { success: false, message: '该数据不存在', data: res }
        }
      } else {
        // 查询列表

        console.log(config)
        // config 前端返回过来的数据
        const { page = 1, limit = 5, word, status } = config.query
        let cloneList = JSON.parse(JSON.stringify(listData.list))
        let total = listData.list.length
        let cloneStatus = null
        if (status === '0') {
          cloneStatus = status
          cloneStatus = 2
        }
        // console.log(config)
        if (status * 1 === 1) {
          cloneList = cloneList.filter(item => item.status === status * 1)
          console.log(cloneList)
          total = cloneList.length
        } else if (cloneStatus * 1 === 2) {
          cloneList = cloneList.filter(item => item.status === status * 1)
          total = cloneList.length
        } else {
          cloneList = JSON.parse(JSON.stringify(listData.list))
          total = cloneList.length
        }
        if (word) {
          cloneList = cloneList.filter(item => item.word.includes(word))
          total = cloneList.length
        }
  
        // list 是请求数据渲染的当前页数据
        const list = cloneList.splice((page - 1) * limit, limit)
        // 注意这里是 listData.list 的长度，和上面的list 不是一个list，是总数据的长度
        return {
          success: true,
          message: '',
          data: {
            list,
            total
          }
        }
      }
    }
  },
  // 添加接口
  {
    url: '/mock/content/inerview',
    type: 'post',
    response: config => {
      // 我们调用接口发现数据在config的body里面，结构出来，追加至数组
      console.log(config)
      const { body } = config
      body.status = body.status === undefined ? 0 : body.status
      body.id = listData.list[listData.list.length - 1].id * 1 + 1
      listData.list.push(body)
      return { success: true, message: '添加成功', data: '' }
    }
  },
  // 修改接口
  {
    url: '/mock/content/inerview',
    type: 'put',
    response: config => {
      console.log(config)
      const { body } = config
      if (!body.id) {
        return { success: false, message: '请提交ID', data: '' }
      }
      // 找到对应元素
      const index = listData.list.findIndex(item => item.id * 1 === body.id * 1)
      // 如果没有找到
      if (index === -1) return { success: false, message: '没有该数据', data: '' }
      // 修改数据
      listData.list[index] = body
      return { success: true, message: '修改成功', data: '' }
    }
  },
  // 删除接口
  {
    url: '/mock/content/inerview',
    type: 'delete',
    response: config => {
      const { query } = config
      // console.log(config)
      if (!query.id) {
        return { success: false, message: '请提交ID', data: '' }
      }
      // 找到对应元素
      const index = listData.list.findIndex(item => item.id * 1 === query.id * 1)
      // 如果没有找到
      if (index === -1) return { success: false, message: '没有该数据', data: '' }
      // 修改数据
      listData.list.splice(index, 1)
      return { success: true, message: '修改成功', data: '' }
    }
  }
]

